"""
Main entry point for the stock data display backend application.
"""
import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.api.stock_routes import router as stock_router
from app.api.prediction_routes import router as prediction_router
from app.core.logging_config import RequestLoggingMiddleware, setup_logging

# 设置日志配置
setup_logging()

# Create FastAPI application instance
app = FastAPI(
    title="Stock Data Display API",
    description="API for displaying stock data with multiple data sources",
    version="1.0.0"
)

# Add request logging middleware
app.add_middleware(RequestLoggingMiddleware)

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000",    # Vite dev server default port
        "http://localhost:5173",    # Vite dev server alternative port
        "http://127.0.0.1:3000",    # Alternative localhost format
        "http://127.0.0.1:5173",    # Alternative localhost format
    ],
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    allow_headers=[
        "Accept",
        "Accept-Language",
        "Content-Language",
        "Content-Type",
        "Authorization",
        "X-Requested-With",
    ],
    expose_headers=["Content-Disposition"],  # For CSV export downloads
)

# Include routers
app.include_router(stock_router)
app.include_router(prediction_router)


@app.get("/")
async def root():
    """Root endpoint for health check"""
    return {"message": "Stock Data Display API is running"}


@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {"status": "healthy", "service": "stock-data-display"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)